import auth from "@/plugins/auth";
import router, { constantRoutes, dynamicRoutes } from "@/router";
import { getAuthority } from "@/utils/auth";
import { getRouters } from "@/api/menu";
import Layout from "@/layout/index";
import ParentView from "@/components/ParentView";
import InnerLink from "@/layout/components/InnerLink";

// 匹配views里面所有的.vue文件
const modules = import.meta.glob("./../../views/**/*.vue");

const usePermissionStore = defineStore("permission", {
  state: () => ({
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
  }),
  actions: {
    setRoutes(routes) {
      this.addRoutes = routes;
      this.routes = constantRoutes.concat(routes);
    },
    setDefaultRoutes(routes) {
      this.defaultRoutes = constantRoutes.concat(routes);
    },
    setTopbarRoutes(routes) {
      this.topbarRouters = routes;
    },
    setSidebarRouters(routes) {
      this.sidebarRouters = routes;
    },
    generateRoutes(roles) {
      return new Promise((resolve) => {
        // 向后端请求路由数据
        let data = localStorage.getItem("authority");
        let res = {
          data: JSON.parse(data),
        };
        res.data.forEach((item, index) => {
          switch (item.meta.title) {
            case "产品工厂":
              item.children.push(
                {
                  name: "AddMaterial",
                  path: "addMaterial",
                  hidden: true,
                  component: "product-factory/product/addMaterial",
                  meta: {
                    title: "贷款产品详情",
                    icon: "peoples",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "AddAudit",
                  path: "addAudit",
                  hidden: true,
                  component: "product-factory/audit/addAudit",
                  meta: {
                    title: "借款产品详情",
                    icon: "peoples",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "RPA自动审批":
              item.children.push(
                {
                  name: "CreditDetail",
                  path: "credit-detail",
                  hidden: true,
                  component: "auto-credit/automatic/detailpage",
                  meta: {
                    title: "详情",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "CreditMessage",
                  path: "credit-message",
                  hidden: true,
                  component: "auto-credit/automatic/message",
                  meta: {
                    title: "详细信息",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "CreditArchives",
                  path: "credit-archives",
                  hidden: true,
                  component: "auto-credit/automatic/particulars",
                  meta: {
                    title: "完整档案",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "保前管理":
              item.children.push(
                {
                  name: "Detailpage",
                  path: "detailpage",
                  hidden: true,
                  component: "loan-before/components/detailpage",
                  meta: {
                    title: "详情",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                },

                {
                  name: "Particulars",
                  path: "particulars",
                  hidden: true,
                  component: "loan-before/components/index",
                  meta: {
                    title: "详细信息",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Archives",
                  path: "archives",
                  hidden: true,
                  component: "loan-before/components/particulars",
                  meta: {
                    title: "完整档案",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "保中管理":
              item.children.push(
                {
                  name: "ContractContents",
                  path: "contract-contents",
                  hidden: true,
                  component: "loan-progress/contract/contents",
                  meta: {
                    title: "合同管理登记",
                    icon: "code",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "ApproveContents",
                  path: "approve-contents",
                  hidden: true,
                  component: "loan-progress/approve/contents",
                  meta: {
                    title: "放款审批登记",
                    icon: "swagger",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "ProcessContents",
                  path: "process-contents",
                  hidden: true,
                  component: "loan-progress/process/contents",
                  meta: {
                    title: "放款登记",
                    icon: "swagger",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "保后管理":
              item.children.push(
                {
                  name: "Paydetail",
                  path: "paydetail",
                  hidden: true,
                  component: "after-loan/pay/paydetail",
                  meta: {
                    title: "还款信息",
                    icon: "build",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Manage",
                  path: "manage",
                  hidden: true,
                  component: "after-loan/refund/manage",
                  meta: {
                    title: "还款详情",
                    icon: "swagger",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Entering",
                  path: "entering",
                  hidden: true,
                  component: "after-loan/check/entering",
                  meta: {
                    title: "保后任务",
                    icon: "tree-table",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Posp",
                  path: "posp",
                  hidden: true,
                  component: "after-loan/finish/posp",
                  meta: {
                    title: "保后信息",
                    icon: "switch",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "催收管理":
              item.children.push(
                {
                  name: "SoonDetail",
                  path: "soon-detail",
                  hidden: true,
                  component: "collection/soon/detail",
                  meta: {
                    title: "详情",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "ExpireDetail",
                  path: "expire-detail",
                  hidden: true,
                  component: "collection/expire/detail",
                  meta: {
                    title: "详情",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "RecordDetail",
                  path: "record-detail",
                  hidden: true,
                  component: "collection/record/detail",
                  meta: {
                    title: "详情",
                    icon: "job",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "系统管理":
              res.data.splice(index, 1);
              break;
            case "客户档案管理":
              item.children.push(
                {
                  name: "Infos",
                  path: "infos",
                  hidden: true,
                  component: "customer-record/access/infos",
                  meta: {
                    title: "客户资料详情",
                    icon: "user",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Personal",
                  path: "personal",
                  hidden: true,
                  component: "customer-record/access/personal",
                  meta: {
                    title: "担保个人信息维护",
                    icon: "build",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Map",
                  path: "map",
                  hidden: true,
                  component: "customer-record/access/map",
                  meta: {
                    title: "地图",
                    icon: "build",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "PubliCorporate",
                  path: "public-corporate",
                  hidden: true,
                  component: "customer-record/access/corporate",
                  meta: {
                    title: "担保对公信息维护",
                    icon: "build",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
            case "数据分析":
              break;
            case "系统权限":
              item.children.push(
                {
                  name: "Member",
                  path: "member",
                  hidden: true,
                  component: "system/staff/addmember",
                  meta: {
                    title: "添加成员",
                    icon: "code",
                    noCache: false,
                    link: null,
                  },
                },
                {
                  name: "Setting",
                  path: "setting",
                  hidden: true,
                  component: "system/role/setting",
                  meta: {
                    title: "权限设置",
                    icon: "code",
                    noCache: false,
                    link: null,
                  },
                }
              );
              break;
          }
        });

        res.data.push(
          {
            name: "Maintain",
            path: "/maintain",
            hidden: true,
            redirect: "noRedirect",
            component: "Layout",
            alwaysShow: true,
            meta: {
              title: "担保信息维护",
              icon: "tool",
              noCache: false,
              link: null,
            },
            children: [
              {
                name: "Maintain",
                path: "maintain",
                hidden: true,
                component: "info-maintain/index",
                meta: {
                  title: "担保信息维护",
                  icon: "build",
                  noCache: false,
                  link: null,
                },
              },
              {
                name: "Person",
                path: "person",
                hidden: true,
                component: "info-maintain/person",
                meta: {
                  title: "担保个人信息维护",
                  icon: "build",
                  noCache: false,
                  link: null,
                },
              },
              {
                name: "Public",
                path: "public",
                hidden: true,
                component: "info-maintain/public",
                meta: {
                  title: "担保对公信息维护",
                  icon: "build",
                  noCache: false,
                  link: null,
                },
              },
            ],
          },
          {
            id: 1,
            name: "Device",
            path: "/device",
            component: "Layout",
            level: 1,
            status: 1,
            sort: "100",
            hidden: false,
            children: [
              {
                name: "DeviceCode",
                path: "deviceCode",
                hidden: false,
                component: "device/code/index",
                meta: {
                  title: "激活码管理",
                  icon: "peoples",
                  noCache: false,
                  link: null,
                },
              },
              {
                name: "DevicePad",
                path: "devicePad",
                hidden: false,
                component: "device/pad/index",
                meta: {
                  title: "PAD管理",
                  icon: "peoples",
                  noCache: false,
                  link: null,
                },
              },
              {
                name: "DeviceApp",
                path: "deviceApp",
                hidden: false,
                component: "device/app/index",
                meta: {
                  title: "应用管理",
                  icon: "peoples",
                  noCache: false,
                  link: null,
                },
              },{
                name: "DeviceGrid",
                path: "deviceGrid",
                hidden: false,
                component: "device/grid/index",
                meta: {
                  title: "网格化管理",
                  icon: "peoples",
                  noCache: false,
                  link: null,
                },
              }
            ],
            select: false,
            meta: {
              title: "设备管理",
              icon: "system",
            },
          }
          // {
          //   name: "FacilitySys",
          //   path: "/facilitySys",
          //   hidden: false,
          //   redirect: "noRedirect",
          //   component: "Layout",
          //   meta: {
          //     title: "设备管理",
          //     icon: "build",
          //     noCache: false,
          //     link: null,
          //   },
          //   children: [
          //     {
          //       name: "Facility",
          //       path: "facility",
          //       hidden: false,
          //       component: "facility/index",
          //       meta: {
          //         title: "设备管理",
          //         icon: "build",
          //         noCache: false,
          //         link: null,
          //       },
          //     },
          //   ],
          // }
        );

        res.data.splice(3, 0, {
          name: "Guarantee",
          path: "/guarantee",
          hidden: false,
          redirect: "noRedirect",
          component: "Layout",
          alwaysShow: true,
          meta: {
            title: "保前尽调",
            icon: "tool",
            noCache: false,
            link: null,
          },
          children: [
            {
              name: "Private",
              path: "private",
              hidden: false,
              component: "guarantee/privates/list",
              meta: {
                title: "个人尽调",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "Corporate",
              path: "corporate",
              hidden: false,
              component: "guarantee/corporate/list",
              meta: {
                title: "企业尽调",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "Private-Detail",
              path: "privateDetail",
              hidden: true,
              component: "guarantee/privates/index",
              meta: {
                title: "个人尽调详情",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "Corporate-Detail",
              path: "corporateDetail",
              hidden: true,
              component: "guarantee/corporate/index",
              meta: {
                title: "企业尽调详情",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
          ],
        });
        res.data.splice(6, 0, {
          name: "Inspect",
          path: "/inspect",
          hidden: false,
          redirect: "noRedirect",
          component: "Layout",
          alwaysShow: true,
          meta: {
            title: "保后检查",
            icon: "tool",
            noCache: false,
            link: null,
          },
          children: [
            {
              name: "First",
              path: "first",
              hidden: false,
              component: "inspect/loan-first/list",
              meta: {
                title: "保后首次检查",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "First-Detail",
              path: "firstDetail",
              hidden: true,
              component: "inspect/loan-first/index",
              meta: {
                title: "保后首次检查详情",
                icon: "build",
                noCache: false,
                link: null,
              },
            },

            {
              name: "Routine",
              path: "routine",
              hidden: false,
              component: "inspect/loan-routine/list",
              meta: {
                title: "保后常规检查",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "Routine-Detail",
              path: "routineDetail",
              hidden: true,
              component: "inspect/loan-routine/index",
              meta: {
                title: "保后常规检查详情",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "Mortgage",
              path: "mortgage",
              hidden: false,
              component: "inspect/loan-mortgage/list",
              meta: {
                title: "按揭楼盘检查",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
            {
              name: "Mortgage-Detail",
              path: "mortgageDetail",
              hidden: true,
              component: "inspect/loan-mortgage/index",
              meta: {
                title: "按揭楼盘检查详情",
                icon: "build",
                noCache: false,
                link: null,
              },
            },
          ],
        });
        // let res = {
        // data: [
        //   {
        //     name: "ProductFactory",
        //     path: "/product-factory",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "产品工厂",
        //       icon: "system",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Parameter",
        //         path: "parameter",
        //         hidden: false,
        //         component: "product-factory/parameter/index",
        //         meta: {
        //           title: "产品参数配置",
        //           icon: "user",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Material",
        //         path: "material",
        //         hidden: false,
        //         component: "product-factory/material/index",
        //         meta: {
        //           title: "贷款材料管理",
        //           icon: "user",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Product",
        //         path: "product",
        //         hidden: false,
        //         component: "product-factory/product/index",
        //         meta: {
        //           title: "贷款产品管理",
        //           icon: "peoples",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "AddMaterial",
        //         path: "addMaterial",
        //         hidden: true,
        //         component: "product-factory/product/addMaterial",
        //         meta: {
        //           title: "贷款产品详情",
        //           icon: "peoples",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       // {
        //       //   name: "Template",
        //       //   path: "template",
        //       //   hidden: false,
        //       //   component: "product-factory/template/index",
        //       //   meta: {
        //       //     title: "合同模板管理",
        //       //     icon: "tree-table",
        //       //     noCache: false,
        //       //     link: null,
        //       //   },
        //       // },
        //       // {
        //       //   name: "AddTemplate",
        //       //   path: "addTemplate",
        //       //   hidden: true,
        //       //   component: "product-factory/template/addTemplate",
        //       //   meta: {
        //       //     title: "新增合同模板",
        //       //     icon: "tree-table",
        //       //     noCache: false,
        //       //     link: null,
        //       //   },
        //       // },
        //       {
        //         name: "Audit",
        //         path: "audit",
        //         hidden: false,
        //         component: "product-factory/audit/index",
        //         meta: {
        //           title: "贷款产品审核",
        //           icon: "tree",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "AddAudit",
        //         path: "addAudit",
        //         hidden: true,
        //         component: "product-factory/audit/addAudit",
        //         meta: {
        //           title: "贷款产品详情",
        //           icon: "tree",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "LoanBefore",
        //     path: "/loan-before",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "贷前管理",
        //       icon: "monitor",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        // {
        //         name: "Automatic",
        //         path: "automatic",
        //         hidden: false,
        //         component: "loan-before/automatic/index",
        //         meta: {
        //           title: "自动授信",
        //           icon: "online",
        //           noCache: false,
        //           link: null,
        //         },
        // },
        //       {
        //         name: "Management",
        //         path: "management",
        //         hidden: false,
        //         component: "loan-before/management/index",
        //         meta: {
        //           title: "准入授信管理",
        //           icon: "online",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Apply",
        //         path: "apply",
        //         hidden: false,
        //         component: "loan-before/apply/index",
        //         meta: {
        //           title: "贷款申请管理",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Detailpage",
        //         path: "detailpage",
        //         hidden: true,
        //         component: "loan-before/management/detailpage",
        //         meta: {
        //           title: "准入授信详情",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Detail",
        //         path: "detail",
        //         hidden: true,
        //         component: "loan-before/apply/detailpage",
        //         meta: {
        //           title: "贷款授信详情",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Particulars",
        //         path: "particulars",
        //         hidden: true,
        //         component: "loan-before/components/index",
        //         meta: {
        //           title: "详细信息",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Archives",
        //         path: "archives",
        //         hidden: true,
        //         component: "loan-before/components/particulars",
        //         meta: {
        //           title: "完整档案",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "LoanProgress",
        //     path: "/loan-progress",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "贷中管理",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Credit",
        //         path: "credit",
        //         hidden: false,
        //         component: "loan-progress/credit/index",
        //         meta: {
        //           title: "产品授信",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "CreditContents",
        //         path: "credit-contents",
        //         hidden: true,
        //         component: "loan-progress/credit/contents",
        //         meta: {
        //           title: "产品授信登记",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Contract",
        //         path: "contract",
        //         hidden: false,
        //         component: "loan-progress/contract/index",
        //         meta: {
        //           title: "合同管理",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "ContractContents",
        //         path: "contract-contents",
        //         hidden: true,
        //         component: "loan-progress/contract/contents",
        //         meta: {
        //           title: "合同管理登记",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Approve",
        //         path: "approve",
        //         hidden: false,
        //         component: "loan-progress/approve/index",
        //         meta: {
        //           title: "放款审批",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "ApproveContents",
        //         path: "approve-contents",
        //         hidden: true,
        //         component: "loan-progress/approve/contents",
        //         meta: {
        //           title: "放款审批登记",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Process",
        //         path: "process",
        //         hidden: false,
        //         component: "loan-progress/process/index",
        //         meta: {
        //           title: "放款处理",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "ProcessContents",
        //         path: "process-contents",
        //         hidden: true,
        //         component: "loan-progress/process/contents",
        //         meta: {
        //           title: "放款登记",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "ProDetail",
        //         path: "proDetail",
        //         hidden: true,
        //         component: "loan-progress/credit/detailpage",
        //         meta: {
        //           title: "产品授信详情",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Information",
        //         path: "information",
        //         hidden: true,
        //         component: "loan-progress/components/index",
        //         meta: {
        //           title: "详细信息",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "ProArchives",
        //         path: "proArchives",
        //         hidden: true,
        //         component: "loan-progress/components/particulars",
        //         meta: {
        //           title: "完整档案",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "AfterLoan",
        //     path: "/after-loan",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "保后管理",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Pay",
        //         path: "pay",
        //         hidden: false,
        //         component: "after-loan/pay/index",
        //         meta: {
        //           title: "还款管理",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Paydetail",
        //         path: "paydetail",
        //         hidden: true,
        //         component: "after-loan/pay/paydetail",
        //         meta: {
        //           title: "还款信息",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Refund",
        //         path: "refund",
        //         hidden: false,
        //         component: "after-loan/refund/index",
        //         meta: {
        //           title: "提前还款",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Manage",
        //         path: "manage",
        //         hidden: true,
        //         component: "after-loan/refund/manage",
        //         meta: {
        //           title: "还款详情",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Check",
        //         path: "check",
        //         hidden: false,
        //         component: "after-loan/check/index",
        //         meta: {
        //           title: "保后检查",
        //           icon: "tree-table",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Entering",
        //         path: "entering",
        //         hidden: true,
        //         component: "after-loan/check/entering",
        //         meta: {
        //           title: "保后报告",
        //           icon: "tree-table",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Warning",
        //         path: "warning",
        //         hidden: false,
        //         component: "after-loan/warning/index",
        //         meta: {
        //           title: "保后预警",
        //           icon: "server",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Finish",
        //         path: "finish",
        //         hidden: false,
        //         component: "after-loan/finish/index",
        //         meta: {
        //           title: "保后结项",
        //           icon: "switch",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Posp",
        //         path: "posp",
        //         hidden: true,
        //         component: "after-loan/finish/posp",
        //         meta: {
        //           title: "保后信息",
        //           icon: "switch",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "Collection",
        //     path: "/collection",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "催收管理",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Soon",
        //         path: "soon",
        //         hidden: false,
        //         component: "collection/soon/index",
        //         meta: {
        //           title: "即将到期",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "SoonDetail",
        //         path: "soon-detail",
        //         hidden: true,
        //         component: "collection/soon/detail",
        //         meta: {
        //           title: "贷款详情",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Expire",
        //         path: "expire",
        //         hidden: false,
        //         component: "collection/expire/index",
        //         meta: {
        //           title: "已逾期",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },

        //       {
        //         name: "ExpireDetail",
        //         path: "expire-detail",
        //         hidden: true,
        //         component: "collection/expire/detail",
        //         meta: {
        //           title: "贷款详情",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Record",
        //         path: "record",
        //         hidden: false,
        //         component: "collection/record/index",
        //         meta: {
        //           title: "催收记录",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "RecordDetail",
        //         path: "record-detail",
        //         hidden: true,
        //         component: "collection/record/detail",
        //         meta: {
        //           title: "贷款详情",
        //           icon: "job",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "Business",
        //     path: "/business",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "运营管理",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "SystemMessage",
        //         path: "system-message",
        //         hidden: false,
        //         component: "business/system-message/index",
        //         meta: {
        //           title: "系统消息",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "ShortMessage",
        //         path: "short-message",
        //         hidden: false,
        //         component: "business/short-message/index",
        //         meta: {
        //           title: "短信消息",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "WebMessage",
        //         path: "web-message",
        //         hidden: false,
        //         component: "business/web-message/index",
        //         meta: {
        //           title: "站内消息",
        //           icon: "swagger",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "CustomerRecord",
        //     path: "/customer-record",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "客户档案管理",
        //       icon: "peoples",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Access",
        //         path: "access",
        //         hidden: false,
        //         component: "customer-record/access/index",
        //         meta: {
        //           title: "已准入客户管理",
        //           icon: "user",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Infos",
        //         path: "infos",
        //         hidden: true,
        //         component: "customer-record/access/infos",
        //         meta: {
        //           title: "客户资料详情",
        //           icon: "user",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Blacklist",
        //         path: "blacklist",
        //         hidden: false,
        //         component: "customer-record/blacklist/index",
        //         meta: {
        //           title: "黑名单客户",
        //           icon: "row",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Tag",
        //         path: "tag",
        //         hidden: false,
        //         component: "customer-record/tag/index",
        //         meta: {
        //           title: "客户标签",
        //           icon: "post",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "Data",
        //     path: "/data",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "数据分析",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Statistics",
        //         path: "statistics",
        //         hidden: false,
        //         component: "data/statistics/index",
        //         meta: {
        //           title: "贷款统计",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Analyse",
        //         path: "analyse",
        //         hidden: false,
        //         component: "data/analyse/index",
        //         meta: {
        //           title: "贷款产品分析",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        //   },
        //   {
        //     name: "System",
        //     path: "/system",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "系统权限",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //     children: [
        //       {
        //         name: "Department",
        //         path: "department",
        //         hidden: false,
        //         component: "system/department/index",
        //         meta: {
        //           title: "机构管理",
        //           icon: "build",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Role",
        //         path: "role",
        //         hidden: false,
        //         component: "system/role/index",
        //         meta: {
        //           title: "角色管理",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Staff",
        //         path: "staff",
        //         hidden: false,
        //         component: "system/staff/index",
        //         meta: {
        //           title: "员工管理",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Member",
        //         path: "member",
        //         hidden: true,
        //         component: "system/staff/addmember",
        //         meta: {
        //           title: "添加成员",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //       {
        //         name: "Setting",
        //         path: "setting",
        //         hidden: true,
        //         component: "system/role/setting",
        //         meta: {
        //           title: "权限设置",
        //           icon: "code",
        //           noCache: false,
        //           link: null,
        //         },
        //       },
        //     ],
        // {
        //     name: "Inspect",
        //     path: "/inspect",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "保后检查",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //   },
        // {
        //     name: "Maintain",
        //     path: "/maintain",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "担保人信息维护",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //   },
        // {
        //     name: "Guarantee",
        //     path: "/guarantee",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "担保尽调",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //   },
        // {
        //     name: "Progress",
        //     path: "/progress",
        //     hidden: false,
        //     redirect: "noRedirect",
        //     component: "Layout",
        //     alwaysShow: true,
        //     meta: {
        //       title: "担保进度查询",
        //       icon: "tool",
        //       noCache: false,
        //       link: null,
        //     },
        //   },
        // ];
        // };
        // getRouters().then(res => {
        const sdata = JSON.parse(JSON.stringify(res.data));
        const rdata = JSON.parse(JSON.stringify(res.data));
        const defaultData = JSON.parse(JSON.stringify(res.data));
        const sidebarRoutes = filterAsyncRouter(sdata);
        const rewriteRoutes = filterAsyncRouter(rdata, false, true);
        const defaultRoutes = filterAsyncRouter(defaultData);
        const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
        asyncRoutes.forEach((route) => {
          router.addRoute(route);
        });
        this.setRoutes(rewriteRoutes);
        this.setSidebarRouters(constantRoutes.concat(sidebarRoutes));
        this.setDefaultRoutes(sidebarRoutes);
        this.setTopbarRoutes(defaultRoutes);
        resolve(rewriteRoutes);
        // })
      });
    },
  },
});

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  return asyncRouterMap.filter((route) => {
    if (type && route.children) {
      route.children = filterChildren(route.children);
    }
    if (route.component) {
      // Layout ParentView 组件特殊处理
      if (route.component === "Layout") {
        route.component = Layout;
      } else if (route.component === "ParentView") {
        route.component = ParentView;
      } else if (route.component === "InnerLink") {
        route.component = InnerLink;
      } else {
        route.component = loadView(route.component);
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children, route, type);
    } else {
      delete route["children"];
      delete route["redirect"];
    }
    return true;
  });
}

function filterChildren(childrenMap, lastRouter = false) {
  var children = [];
  childrenMap.forEach((el, index) => {
    if (el.children && el.children.length) {
      if (el.component === "ParentView" && !lastRouter) {
        el.children.forEach((c) => {
          c.path = el.path + "/" + c.path;
          if (c.children && c.children.length) {
            children = children.concat(filterChildren(c.children, c));
            return;
          }
          children.push(c);
        });
        return;
      }
    }
    if (lastRouter) {
      el.path = lastRouter.path + "/" + el.path;
    }
    children = children.concat(el);
  });
  return children;
}

// 动态路由遍历，验证是否具备权限
export function filterDynamicRoutes(routes) {
  const res = [];
  routes.forEach((route) => {
    if (route.permissions) {
      if (auth.hasPermiOr(route.permissions)) {
        res.push(route);
      }
    } else if (route.roles) {
      if (auth.hasRoleOr(route.roles)) {
        res.push(route);
      }
    }
  });
  return res;
}

export const loadView = (view) => {
  let res;
  for (const path in modules) {
    const dir = path.split("views/")[1].split(".vue")[0];
    if (dir === view) {
      res = () => modules[path]();
    }
  }
  return res;
};

export default usePermissionStore;
